package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.PageInfo;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Integer selectCountUser() {
        return userMapper.selectCountUser();
    }

    @Override
    public PageInfo selectLimit(Integer current, Integer size) {
        //总条数
        Integer total = userMapper.selectCountUser();
        //总页码
        int pages = total % size == 0 ? total / size : total / size + 1;
        //对当前页码进行判断
        if (current < 1) {
            current = 1;
        }
        if (current > pages) {
            current = pages;
        }
        int index = (current - 1) * size;
        List<User> users = userMapper.selectLimit(index, size);
        return new PageInfo(total,pages,current,users);
    }
}
